<template>
  <div class="login-container">
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="16" :xl="16">
        <div style="color: transparent">占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          label-position="left"
        >
          <div class="title" />
          <div class="title-tips"><img src="../../assets/logo.png" alt="">Piece 考试</div>
          <el-form-item style="margin-top: 40px" prop="uname">
            <span class="svg-container svg-container-admin">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="uname"
              v-model.trim="loginForm.uname"
              v-focus
              placeholder="请输入用户名"
              tabindex="1"
              type="text"
            />
          </el-form-item>
          <el-tooltip v-model="capsTooltip" content="大写锁定已打开" placement="right" manual>
            <el-form-item prop="pword">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="pword"
                v-model.trim="loginForm.pword"
                :type="passwordType"
                tabindex="2"
                placeholder="请输入密码"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
              />
              <span
                v-if="passwordType === 'password'"
                class="show-password"
                @click="handlePassword"
              >
                <svg-icon icon-class="eye" />
              </span>
              <span v-else class="show-password" @click="handlePassword">
                <svg-icon icon-class="eye-open" />
              </span>
            </el-form-item>
          </el-tooltip>
          <el-form-item prop="verify">
            <el-row>
              <el-col :span="14">
                <span class="svg-container">
                  <svg-icon icon-class="validCode" />
                </span>
                <el-input v-model.trim="loginForm.captchaCode" tabindex="3" placeholder="请输入验证码" @keyup.enter.native="handleLogin" />
              </el-col>
              <el-col :span="10">
                <img ref="verify_code" style="width: 130px;cursor: pointer;" alt="点击切换" :src="verifyUrl" @click="getVerifyCode">
              </el-col>
            </el-row>
          </el-form-item>
          <el-row style="display:inline;">
            <el-button
              :loading="loading"
              class="login-btn"
              type="primary"
              @click="handleLogin"
            >
              登录
            </el-button>
            <el-dropdown @command="showSettings = true">
              <el-button size="normal" type="info" icon="el-icon-setting" />
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item> 设置 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-row>

          <el-divider>
            还没有账号？
            <router-link to="/register">
              <el-button type="text">注 册</el-button>
            </router-link>
          </el-divider>
          <br>
          <el-divider v-if="oauthLink.length > 0">链接登陆</el-divider>
          <el-row v-if="oauthLink.length > 0" style="display:inline;">
            <div class="outside-login">
              <a
                v-for="link in oauthLink"
                :key="link.name"
                :class="'outside-login-btn ' + link.name"
                target="_Blank"
                :href="getOauthLink(link.name)"
              >
                <em><i :class="'fa fa-' + link.name" /></em>
              </a>
            </div>
          </el-row>
        </el-form>
      </el-col>
      <!--  底部  -->
      <el-col :span="24">
        <div class="el-login-footer">
          <span>Copyright © {{ copyright }} {{ siteName }} All Rights Reserved.</span>
        </div>
      </el-col>
    </el-row>
    <right-panel v-if="showSettings" :show="showSettings" @close="handleCloseSetting">
      <div class="right-panel-slot">
        <el-dropdown @command="handleCommand">
          <el-button type="primary">
            切换服务器地址<i class="el-icon-arrow-down el-icon--right" />
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="a"> 自动 </el-dropdown-item>
            <el-dropdown-item command="b">局域网</el-dropdown-item>
            <el-dropdown-item command="c">公有云</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </right-panel>
  </div>
</template>

<script>
import { getUUID } from '@/utils/auth'
import RightPanel from '@/components/RightPanel'

export default {
  name: 'Login',
  components: {
    RightPanel
  },
  directives: {
    focus: {
      inserted(el) {
        el.querySelector('input').focus()
      }
    }
  },
  data() {
    return {
      loginForm: {
        uname: '',
        pword: '',
        captchaCode: '',
        captcha_token: ''
      },
      loginRules: {
        uname: [{ required: true, trigger: 'blur', message: '请输入登录名' }],
        pword: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        captchaCode: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      capsTooltip: false,
      redirect: undefined,
      verifyUrl: '',
      siteName: '',
      copyright: '',
      oauthLink: []
    }
  },
  computed: {
    showSettings: {
      get() {
        return this.$store.state.settings.showSettings
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'showSettings',
          value: true
        })
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    document.body.style.overflow = 'hidden'
  },
  mounted() {
    const this_ = this
    this.loginForm.captcha_token = getUUID()
    this.getFooterInfo()
    this.getOauthData()

    if (this.loginForm.uname === '') {
      this.$refs.uname.focus()
    } else if (this.loginForm.pword === '') {
      this.$refs.pword.focus()
    }

    this.getVerifyCode()
    setTimeout(() => {
      this_.$refs.verify_code.click()
    }, 1000)
  },
  methods: {
    handleCloseSetting() {
      this.$store.dispatch('settings/changeSetting', {
        key: 'showSettings',
        value: false
      })
    },
    handlePassword() {
      this.passwordType === 'password'
        ? (this.passwordType = '')
        : (this.passwordType = 'password')
      this.$nextTick(() => {
        this.$refs.pword.focus()
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    getFooterInfo() {
      this.$http.get('/footer/info').then(response => {
        this.siteName = response.param.siteName
        this.copyright = response.param.copyright
      }).catch(e => {})
    },
    getOauthData() {
      this.$http.get('/oauth/list').then(response => {
        this.oauthLink = response
      }).catch(e => {})
    },
    getOauthLink(type) {
      return this.$store.state.app.server_api + '/oauth/callback/call_' + type
    },
    getVerifyCode() {
      this.verifyUrl = this.$store.state.app.server_api + '/authentication/code/captcha?type=char&captcha_token=' + this.loginForm.captcha_token + '&r=' + Math.random()
    },
    handleLogin() {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.getVerifyCode()
          })
        } else {
          return false
        }
      })
    },
    handleCommand(command) {
      const protocol = window.location.protocol + '//' + window.location.host
      switch (command) {
        case 'a':
          this.$store.dispatch('app/setServieApi', { serverApi: protocol + process.env.VUE_APP_BASE_API })
          break
        case 'b':
          this.$store.dispatch('app/setServieApi', { serverApi: 'http://localhost/xenon' })
          break
        case 'c':
          this.$store.dispatch('app/setServieApi', { serverApi: 'http://localhost' + process.env.VUE_APP_BASE_API })
          break
      }
      this.$message({
        type: 'success',
        message: '成功切换服务器地址'
      })
      this.getVerifyCode()
    }
  }
}
</script>

<style lang="scss" scoped>
  .login-container {
    height: 100vh;
    background: url("~@/assets/bg.jpg") center center fixed no-repeat;
    background-size: cover;

    .title {
      font-size: 54px;
      font-weight: 500;
      color: rgba(14, 18, 26, 1);

      img {
        height: 70px;
      }
    }

    .title-tips {
      margin-top: 10px;
      font-size: 26px;
      font-weight: 400;
      color: rgba(14, 18, 26, 1);
      text-overflow: ellipsis;
      white-space: nowrap;

      img {
        height: 60px;
        margin-right: 15px;
        margin-bottom: -10px;
      }
    }

    .login-btn {
      display: inherit;
      width: 220px;
      height: 40px;
      border: 0;

      &:hover {
        opacity: 0.9;
      }
    }

    .login-form {
      position: relative;
      max-width: 100%;
      margin: calc((100vh - 425px) / 2) 10% 10%;
      overflow: hidden;

      .forget-password {
        width: 100%;
        margin-top: 20px;
        text-align: left;

        .forget-pass {
          width: 129px;
          height: 19px;
          font-size: 20px;
          font-weight: 400;
          color: rgba(92, 102, 240, 1);
        }
      }
    }

    .tips {
      margin-bottom: 10px;
      font-size: 12px;
      color: #ffffff;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .title-container {
      position: relative;

      .title {
        margin: 0 auto 40px auto;
        font-size: 34px;
        font-weight: bold;
        color: #409EFF;
        text-align: center;
      }
    }

    .svg-container {
      position: absolute;
      top: 7px;
      left: 15px;
      z-index: 999;
      font-size: 16px;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    .show-password {
      position: absolute;
      top: 7px;
      right: 25px;
      font-size: 16px;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    ::v-deep {
      .el-form-item {
        padding-right: 0;
        margin: 15px 0;
        color: #454545;
        background: transparent;
        border: 1px solid transparent;
        border-radius: 2px;

        &__content {
          min-height: 32px;
          line-height: 32px;
        }

        &__error {
          position: absolute;
          top: 100%;
          left: 18px;
          font-size: 12px;
          line-height: 18px;
          color: #ff4d4f;
        }
      }

      .el-input {
        box-sizing: border-box;

        input {
          height: 42px;
          padding-left: 45px;
          font-size: 14px;
          line-height: 58px;
          color: #606266;
          background: #f6f4fc;
          border: 0;
          caret-color: #606266;
        }
      }
    }
  }

  .outside-login {
    .outside-login-btn em {
    display: block;
    width: 30px;
    height: 30px;
    line-height: 30px;
    border-radius: 50%;
    margin: 0 auto 5px;
    white-space: normal;
    font-size: 20px;
    color: #fff;

    .outside-login-btn:first-child, .outside-login-btn:last-child {
      width: 30.3333%;
    }
    .outside-login-btn span {
      font-size: 14px;
      color: #333;
    }
    .weixin em{
      background-color: #4ec34d;
    }
    .qq em{
      background-color:  #18c8f6;
    }
    .weibo em{
      background-color: #ff4700
    }
  }
}
</style>

<style lang="scss" scoped>
  .right-panel-slot {
    margin-top: 45px;
    margin-left: 15px;
  }
  .drawer-container {
    margin-top: 50px;
    margin-left: 15px;
    padding: 24px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;

    .drawer-title {
      margin-bottom: 12px;
      color: rgba(0, 0, 0, .85);
      font-size: 14px;
      line-height: 22px;
    }

    .drawer-item {
      color: rgba(0, 0, 0, .65);
      font-size: 14px;
      padding: 12px 0;
    }

    .drawer-switch {
      float: right;
    }
  }
  .el-select-dropdown, .el-popper {
    z-index: 100000 !important;
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    background-color: rgba(0,0,0,0.4);
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
</style>
